<?php 
Class appController extends Controller
{
	
	
    public function _initialize()
	{
		//检查登录
		$this->check_admin();
        $this -> assign('_L',lang());//赋值语言项
        $path=SUBDIR."/Admin/Webroot/";
        C('hy_path',$path);
        $_cfg=C();
        $_cfg['DIGLOSSIA']=DIGLOSSIA;//双语配置
        $_cfg['DEBUG']=DEBUG;//开发者
        $this-> assign('_C',$_cfg);
        $this-> assign('_GET',$_GET);
        if(isset($_SESSION['admin_id']))
        {
        	$this->setside();
        }
	}
	/**
	 *  权限管理
	 * @access public
	 * @param $action 权限名
	 *  @param $type 消息类型
	 * @return void
	 */
	public function admin_priv($action, $type=1)
	{
		$priv_id = Model('app')->get_action_id($action);
		if ($_SESSION['admin_action'] == '-1')
		{
			return true;
		}
		$admin_mod = explode(",", $_SESSION['admin_mod']);
		if (!in_array($priv_id, $admin_mod))
		{
			if($_POST)
			{
				$result = array('error' => 1, 'content' => Lang('PERMISSION_DENIED'));
				$this->showMsg($result);
			}
			else 
			{
				if(strpos($_GET['a'], 'del') !== false)
				{
					$result = array('error' => 1, 'content' => Lang('PERMISSION_DENIED'));
					$this->showMsg($result);
				}
				echo "你没有操作的权限";
				exit;
			}
			return false;
		}
		else
		{
			return true;
		}
	}
	
	protected function create_html($activePage,$filename='',$saveDir)
	{
		$static = new StaticPage;
		
		$static->makeStaticPage($activePage,'html',$saveDir,$filename);
	}
	/**
	 *  是否已登录
	 * @access public
	 * @return void
	 */
	public function check_admin()
	{
		if(!Model('app')->check_admin() &&
		ACTION_NAME!= 'login'&&
		ACTION_NAME!= 'act_login'&&
		ACTION_NAME!= 'captcha' )
		{
			header("Location:/qq_admin.php");
		}
		elseif(ACTION_NAME== 'login'||
		ACTION_NAME== 'act_login')
		{	
			if(!is_array($_COOKIE['QIAQIA'])||empty($_COOKIE['QIAQIA']['admin_access']))
			{	
				hy_404();
			}
		}
	}
	
	public function get_cat_id($id,$table)
	{
		return Model('app')->get_cat_id($id,$table);
	}
	
	/**
	 *  创建静态页面
	 * @access protected
	 * @htmlfile 生成的静态文件名称
	 * @htmlpath 生成的静态文件路径
	 * @param string $templateFile 指定要调用的模板文件
	 * 默认为空 由系统自动定位模板文件
	 * @return string
	 */
	protected function buildHtml($htmlfile='',$htmlpath='',$templateFile='') {
		
		$content = $this->fetch($templateFile);
		$htmlpath   = !empty($htmlpath)?$htmlpath:ROOT_PATH;
		$htmlfile =  $htmlpath.$htmlfile.'.html';
		if(!is_dir(dirname($htmlfile)))
			// 如果静态目录不存在 则创建
			mkdir(dirname($htmlfile),0777,true);
		if(false === file_put_contents($htmlfile,$content))
		{
			return false;
		}
		return true;
	}
	
	/**
	 * 获取侧栏信息
	 */
	public function setside()
	{
		if(isset($_SESSION['side']) && !empty($_SESSION['side']))
		{
			$side = $_SESSION['side'];
		}
		else 
		{
			include(ROOT_PATH.'Admin/Config/config.php');
			if($_SESSION['admin_action'] != '-1')
			{
				$action_id = explode(",", $_SESSION['admin_action']);
				$action = Model('app')->get_action_name($action_id);
				foreach($side as $k => $v)
				{
					if(!in_array($v['name'], $action))
					{
						$side[$k]['type'] = 1;
					}
				}
			}
			if(!DEBUG)
			{
                $_SESSION['side'] = $side;
			}
		}
		$this->assign("side", $side);
	}
	
}